package com.example.mycontact

import android.app.Activity
import android.app.ComponentCaller
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mycontact.DB.ContactTable
import com.example.mycontact.DB.MyDBHelper
import com.example.mycontact.bean.Contact
import com.example.mycontact.databinding.ActivityAddContactBinding
import java.io.ByteArrayOutputStream

class AddContactActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var binding: ActivityAddContactBinding
    var newcontact = Contact()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAddContactBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.ibSubmit.setOnClickListener(this)
        binding.ibExit.setOnClickListener(this)
        binding.ibTouxiang.setOnClickListener(this)
    }
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.ib_submit -> {
                newcontact.name = binding.etName.text.toString().trim()
                newcontact.company = binding.etCompany.text.toString().trim()
                newcontact.position = binding.etPosition.text.toString()
                newcontact.tel = binding.etNumber.text.toString()
                newcontact.mail = binding.etMail.text.toString()
                //把输入的数据写入到数据库
                //1.获取数据库管理对象
//                var myDBHelper = MyDBHelper(this)
//                //2.打开数据库
//                var mydb = myDBHelper.writableDatabase
//                //3.写入数据
//                val insertsql = "insert into Contacts(name,company,position,tel,mail) values(${newcontact.name},${newcontact.company},${newcontact.position},${newcontact.tel},${newcontact.mail})"
//                mydb.execSQL(insertsql)
//                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show()
                var contactTable = ContactTable(this)
                if(contactTable.AddnewContact(newcontact)){
                    Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show()
                    //跳转到联系人主界面
                    val intent = Intent(this,MainActivity::class.java)
                    startActivity(intent)
                }else{
                    Toast.makeText(this,"添加失败,请重试",Toast.LENGTH_SHORT).show()
                }
            }
            R.id.ib_exit -> {
                finish()
            }
            R.id.ib_touxiang -> {
                //头像 图片选择
                //打开文件选择器
                var intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
                intent.addCategory(Intent.CATEGORY_OPENABLE)
                intent.type="image/*"
                startActivityForResult(intent,2)
            }
        }
    }

    //重写方法，处理选择图片后，回传的参数
    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?,
        caller: ComponentCaller
    ) {
        when(requestCode){
            2->{
                if(resultCode== Activity.RESULT_OK && data!=null){
                    //把返回的uri格式转换成图片后显示到控件上
                    data.data?.let{
                        var bitmap=getBitmapFromUri(it)
                        binding.ibTouxiang.setImageBitmap(bitmap)
                        //把图片写入数据表
                        //把图片转换成二进制数据
                        var os: ByteArrayOutputStream = ByteArrayOutputStream()
                        bitmap?.compress(Bitmap.CompressFormat.PNG,100,os)
                        newcontact.image = os.toByteArray()
                    }
                }
            }
        }
        true
    }
    fun getBitmapFromUri(uri: Uri)=contentResolver.openFileDescriptor(uri,"r")?.use{
        BitmapFactory.decodeFileDescriptor(it.fileDescriptor)
    }
}